package com.eblacorp.archive.service.impl;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.io.*;

import com.eblacorp.archive.annotations.FnTransactional;
import com.eblacorp.archive.dao.fn.TransactionFileDao;
import com.eblacorp.archive.domain.fn.TransactionFile;
import com.eblacorp.archive.exceptions.NoAttachedFnDocument;
import com.eblacorp.archive.exceptions.NotFnDocumentException;
import com.eblacorp.archive.exceptions.NullValueException;
import com.eblacorp.archive.service.TransactionFileService;
import com.filenet.api.constants.CheckinType;

public class TransactionFileServiceImpl implements TransactionFileService{
	
	private TransactionFileDao transactionFileDao;

	@Override
	@FnTransactional
	public List<TransactionFile> search(TransactionFile transactionFile, Date transactionDateFrom,
			Date transactionDateTo, Integer startPage,
			Integer pageSize,boolean withDocument) throws IllegalArgumentException, NotFnDocumentException, IntrospectionException, IllegalAccessException, InvocationTargetException, InstantiationException {
		transactionFile.setCurrentVersion(true);
		return transactionFileDao.getByExampleAndDate(transactionFile, transactionDateFrom, transactionDateTo, pageSize, pageSize, withDocument);
	}
	
	@Override
	@FnTransactional
	public TransactionFile addNewDocument(TransactionFile transactionFile, File file) throws IllegalArgumentException, NotFnDocumentException, IntrospectionException, IllegalAccessException, InvocationTargetException, InstantiationException, NullValueException, IOException {
		TransactionFile newTransactionFile = null;
		transactionFile.setDocumentTitle(transactionFile.getRealtyRegistrationNumber());
		if(file == null){
			newTransactionFile = transactionFileDao.create(transactionFile, file, CheckinType.MINOR_VERSION);
		}else{
			newTransactionFile = transactionFileDao.create(transactionFile, file, CheckinType.MAJOR_VERSION);
		}
		return newTransactionFile;
	}
	
	@Override
	@FnTransactional
	public void delete(TransactionFile transactionFile,boolean allVersion) throws IllegalArgumentException, IntrospectionException, IllegalAccessException, InvocationTargetException, NullValueException {
		transactionFileDao.delete(transactionFile, allVersion);
		
	}
	
	@Override
	@FnTransactional
	public byte[] download(TransactionFile transactionFile) throws NoAttachedFnDocument, IOException {
		
		return transactionFileDao.download(transactionFile);
	}
	
	@Override
	@FnTransactional
	public TransactionFile updateContent(TransactionFile transactionFile,
			File file) throws IllegalArgumentException, IntrospectionException, IllegalAccessException, InvocationTargetException, NullValueException, IOException {
		transactionFile.setDocumentTitle(transactionFile.getRealtyRegistrationNumber());
		transactionFile = transactionFileDao.update(transactionFile, file, CheckinType.MAJOR_VERSION);
		return transactionFile;
	}

	@Override
	@FnTransactional
	public TransactionFile updateProperties(TransactionFile transactionFile) throws IllegalArgumentException, IntrospectionException, IllegalAccessException, InvocationTargetException, NullValueException, IOException {
		transactionFile.setDocumentTitle(transactionFile.getRealtyRegistrationNumber());
		transactionFile = transactionFileDao.update(transactionFile, null, null);
		return transactionFile;
	}
	
	@Override
	public String getViewerUrl(TransactionFile transactionFile) {
		return transactionFileDao.getWcmJavaViewerUrl(transactionFile);
	}

	public TransactionFileDao getTransactionFileDao() {
		return transactionFileDao;
	}

	public void setTransactionFileDao(TransactionFileDao transactionFileDao) {
		this.transactionFileDao = transactionFileDao;
	}	
}
